package ru.papergames.view.components.field
{
	import caurina.transitions.Tweener;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import ru.papergames.model.vo.BallVO;
	import ru.papergames.model.vo.PositionVO;
	import ru.papergames.view.events.GameUIEvent;
	
		
	public class ActorsHolder extends Sprite
	{	
		
		//x_step		
		private var _x_step:Number = 5;		
		public function get x_step():Number {
			return _x_step;
		} 
		
		public function set x_step(val:Number):void {
			_x_step = val;
		}
		
		//y_step		
		private var _y_step:Number = 5;		
		public function get y_step():Number {
			return _y_step;
		} 
		
		public function set y_step(val:Number):void {
			_y_step = val;
		}
		
		//rows		
		public function get rows():uint {
			return parent ? (parent as FightFieldGrid).rows : 1;
		} 
		
		//cols		
		public function get cols():uint {
			return parent ? (parent as FightFieldGrid).cols : 1;
		} 
		
		
		public function ActorsHolder(cell_width:Number, cell_height:Number)
		{
			super();	
			this.mouseChildren = false;
			this.mouseEnabled = false;	
			
			this.x_step = cell_width;		
			this.y_step = cell_height;
		}	
		
		public function getBusyPositions(exclude:PositionVO = null):Array {
			var positions:Array = [];
			
			for (var i:uint = 0; i < this.numChildren; i++ ) {
				var testClip:BallClip = this.getChildAt(i) as BallClip;
				
				if (exclude && testClip.data.position.equals(exclude) ) continue;
				
				positions.push(testClip.data.position);
				
			}
			
			return positions;			
		}
		
		public function stopAllBallsTweening():void {
			
			for (var i:uint = 0; i < this.numChildren; i++ ) {
				var testClip:BallClip = this.getChildAt(i) as BallClip;
				
				if (testClip) {
					
					Tweener.removeTweens(testClip);
					testClip.alpha = 1;
					
				}
				
			}
		}
		
		public function playBallSelectionAnimation(pos:PositionVO):void {
			
			stopAllBallsTweening();
			
			var clip:BallClip = getBallClip(pos);
			
			if (clip) {			
								
				Tweener.addTween(clip,{ alpha:  1 - clip.alpha,
									time: .4,											
									transition: 'linear', 
									onComplete: playBallSelectionAnimation,
									onCompleteParams: [pos]}
									);
				
			}
		}
		
		public function stopBallSelectionAnimation(pos:PositionVO):void {
			var clip:BallClip = getBallClip(pos);
			
			if (clip) {
				Tweener.removeTweens(clip);
				clip.alpha = 1;
			}
		}
		
		public function removeBallAt(pos:PositionVO):void {
			var clip:BallClip = getBallClip(pos);
			
			if (clip) this.removeChild(clip);
		}
		
		public function isPositionBusy(pos:PositionVO):Boolean {
			var clip:BallClip = getBallClip(pos);
			
			return clip ? true : false;
		}
		
		public function ballMove(route:Array):void {
			
			if (route.length <= 1) {
				
				this.dispatchEvent(new GameUIEvent(GameUIEvent.ANIMATION_COMPLETED));
			
				return;
			}		
						
			var clip:BallClip = getBallClip( route.shift() );			
			
			clip.alpha = 1;
			
			var pointToGo:PositionVO = route[0];	
			
			clip.data.position.x = pointToGo.x;
			clip.data.position.y = pointToGo.y;
			
			Tweener.addTween(clip,{ x: pointToGo.x*x_step + x_step/2, 
									y: pointToGo.y*y_step + y_step/2, 
									time: .5,											
									transition: 'easeInOutCubic', 
									onComplete: ballMove,
									onCompleteParams: [route]}
									);
		}
				
		public function positionateActor(child:DisplayObject, pos:PositionVO):void {
			if (child.parent == this) {
				
				child.x = x_step * pos.x + x_step/2;
				child.y = y_step * pos.y + y_step/2;
				
			}
		}
		
		public function removeAllChildren():void
	    {
	        while (numChildren > 0)
	        {
	            removeChildAt(0);
	        }
	    }
	    
	    
	    public function getBallClip(pos:PositionVO):BallClip {
			
			for (var i:uint = 0; i < this.numChildren; i++ ) {
				var testClip:BallClip = this.getChildAt(i) as BallClip;
				
				if (testClip && testClip.data.position.equals(pos) ) return testClip;
			}
			
			return null;
		}
		
	}
}